package com.neusoft.admin.controller;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

import com.neusoft.admin.entity.TableData;
import com.neusoft.admin.method.GetColor;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.stage.Stage;

public class AQIconfirmViewControl {
    @FXML
    private TableView<TableData> tableView;
    @FXML
    private Button backButton;
    private String id, address, level, time, gridInfo, supervisorInfo, feedback;

    @FXML
    public void initialize() {
        read();
        setTableView();
    }

    public AQIconfirmViewControl(String id, String gridInfo) {
        this.id = id;
        this.gridInfo = gridInfo;
    }

    public AQIconfirmViewControl() {
    }

    public void read() {
        File file = new File("src/com/neusoft/admin/data/mission.txt");
        try (BufferedReader br = new BufferedReader(new FileReader(file))) {
            if (!file.exists()) {
                System.out.println("文件不存在");
                return;
            }
            String line;
            if (id == null) {
                System.out.println("未查询到00");
                return;
            }
            while ((line = br.readLine()) != null) {
                String[] parts = line.split(" ");
                if (parts[0].equals(id)) {
                    supervisorInfo = parts[2];
                    address=parts[6]+parts[7]+parts[9];
                    level = parts[11];
                    feedback = parts[13];
                    time = parts[15];
                    break;
                } else {
                    System.out.println("未查询到");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void setTableView() {
        final double TABLE_HEIGHT = 450;
        final double TABLE_HEADER_HEIGHT = 30;
        final double TABLE_PADDING = 0.1;
        ObservableList<TableData> data = FXCollections.observableArrayList(
                new TableData("确认AQI数据编号", id),
                new TableData("确认信息所在地址", address),
                new TableData("确认AQI等级", level),
                new TableData("确认日期时间", time),
                new TableData("网格员信息", gridInfo),
                new TableData("公众监督员信息", supervisorInfo),
                new TableData("公众监督员反馈信息描述", feedback));
        tableView.setItems(data);
        double rowHeight = (TABLE_HEIGHT - TABLE_HEADER_HEIGHT - TABLE_PADDING) / data.size();
        tableView.setFixedCellSize(rowHeight);

        // 第二列索引为 1
        if (tableView.getColumns().size() > 1) {
            TableColumn<TableData, String> targetColumn = (TableColumn<TableData, String>) tableView.getColumns()
                    .get(1);
            targetColumn.setCellFactory(col -> new TableCell<TableData, String>() {
                @Override
                protected void updateItem(String item, boolean empty) {
                    super.updateItem(item, empty);
                    if (empty || item == null) {
                        setText(null);
                        setStyle("");
                    } else {
                        setText(item);
                        setAlignment(Pos.CENTER);
                        if (getIndex() == 2) {
                            setStyle(GetColor.getColor(item));
                        } else {
                            setStyle("");
                        }
                    }
                }
            });
        }
        for (TableColumn<TableData, ?> column : tableView.getColumns()) {
            column.setSortable(false);
        }
        tableView.setStyle("-fx-font-size: 18px;-fx-background-radius: 15px;");
    }

    public void backButtonAction() {
        Stage stage = (Stage) backButton.getScene().getWindow();
        stage.close();
    }

}
